<template>
  <div id="suppliers">
    <div class="card">
      <div class="card-header bg-info text-white">
        模拟票号的航司
        <small>(对于官方没有提供票号的航司，需要模拟票号以方便录入系统)</small>
      </div>
      <div class="card-body">
        <form class="row">
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>

            <a
              href="javascript:void(0)"
              class="btn btn-success float-end"
              @click="newSupplier()"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>两字代码</th>
          <th>三字代码</th>
          <th>当前票号</th>
          <th>生成时间</th>
          <th>修改时间</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="info in suppliers" :key="info.id">
          <td>{{ info.twoCode }}</td>
          <td>{{ info.threeCode }}</td>
          <td>{{ info.ticketNo }}</td>
          <td>{{ info.createTime }}</td>
          <td>{{ info.lastUpdate }}</td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-prompt ref="modalPrompt" :nullable="modalNullable">
      <span slot="title">{{ modalTitle }}</span>
    </my-modal-prompt>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import MyModalPrompt from "@/components/modal/my-modal-prompt.vue";
import { searchFakedTickets, saveFakedTicket } from "@/api/admin.js";

export default {
  components: {
    MyPagination,
    MyModalPrompt,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },

      twoCode: "",

      modalTitle: "",
      modalNullable: false,

      suppliers: [],
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };
      searchFakedTickets(params, (jsonResult) => {
        this.suppliers = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    newSupplier: function () {
      this.modalTitle = "请输入需要模拟票号的航司的两字代码：";
      this.modalNullable = false;
      this.$refs.modalPrompt
        .modal()
        .then((v) => this.createFakedTicket(v))
        .catch((ex) => {});
    },
    createFakedTicket: function (twoCode) {
      const params = {
        twoCode: twoCode,
      };
      saveFakedTicket(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("保存成功", "success");
          this.search();
        } else {
          this.showErrMsg(
            jsonResult.errmsg + "(" + jsonResult.errcode + ")",
            "danger"
          );
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
